package p.ithorns.framework.core.model;

import java.util.Collection;
import java.util.HashSet;

/**
 * ChangeSet
 *
 * @author HtL
 * @date 2024/7/1 15:47
 * @since 1.0.0
 */
public class ChangeSet<T> {
    /**
     * B相对A增加的项
     */
    private Collection<T> added;

    /**
     * A相对于B删除的项
     */
    private Collection<T> removed;


    private ChangeSet(Collection<T> added, Collection<T> removed) {
        this.added = added;
        this.removed = removed;
    }

    public Collection<T> getAdded() {
        return added;
    }

    public Collection<T> getRemoved() {
        return removed;
    }

    public static <E> ChangeSet<E> computeOf(Collection<E> source, Collection<E> relative) {
        // 计算B中相对A增加的项（集合C）
        HashSet<E> addSet = new HashSet<>(relative);
        addSet.removeAll(source);

        // 计算A中相对B减少的项（集合D），注意这里的操作是setA.removeAll(setB)
        HashSet<E> removeSet = new HashSet<>(source);
        removeSet.removeAll(relative);
        return new ChangeSet<>(addSet, removeSet);
    }

    public static void main(String[] args) {
        // 假设这是集合A和B
        HashSet<Integer> setA = new HashSet<>();
        setA.add(1);
        setA.add(2);
        setA.add(3);
        setA.add(4);

        HashSet<Integer> setB = new HashSet<>();
        setB.add(3);
        setB.add(4);
        setB.add(5);
        setB.add(6);

        ChangeSet<Integer> set = ChangeSet.computeOf(setA, setB);


        // 输出结果
        System.out.println("集合B相对于集合A增加的项（集合C）: " + set.getAdded());
        System.out.println("集合A相对于集合B减少的项（集合D）: " + set.getRemoved());

    }

}